package org.itboys.mobile.controller;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayMemberDto;
import org.itboys.mobile.service.mongo.mobile.SundayMobileBusinessService;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCommonService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：圣代_项目公用__controller
 */
@Controller
@RequestMapping("/sunday/mobile/business")
public class SundayMobileBusinessController extends BaseController{
  @Autowired
  private SundayMobileBusinessService businessService;



    /**
     * 获取首页所有数据
     * @param request
     * @param response
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/getBusinessData", method= RequestMethod.POST)
    @ApiOperation(value="获取商户数据。可动态选择分页或不分页")
    public String getBusinessData(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=false, name="longitude", value="用户经度")@RequestParam(value = "longitude", required = false)Double longitude,
            @ApiParam(required=false, name="latitude", value="用户维度")@RequestParam(value = "latitude", required = false)Double latitude,
            @ApiParam(required=false, name="number", value="最大数量")@RequestParam(value = "number", required = false)Integer number,
            @ApiParam(required=false, name="categoryId", value="分类ID")@RequestParam(value = "categoryId", required = false)String categoryId,
            @ApiParam(required = false, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = false) String pageSize,
            @ApiParam(required = false, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = false) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。销量：saleNumber,价格：price") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,

            Model model
            ){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();

           List<SundayMemberDto> businesses =businessService.getBusiness(request,number,memberId,categoryId);
            model.addAttribute("businesses", businesses);
            model.addAttribute("longitude", longitude);
            model.addAttribute("latitude", latitude);
            return "/pingan/mobile/business/index";
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/org.itboys.mobile.common/getIndexData----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/sunday/mobile/error";
        }
    }

}
